package com.zyjc.city.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * @author ckj
 * @date 2024年04月01日 17:03
 */
public class BeanTransUtil {
    public BeanTransUtil() {
    }

    public static <T> T convert(Object source, Class<T> target) {
        if (source == null) {
            return null;
        } else if (target == null) {
            return null;
        } else {
            try {
                T newInstance = target.newInstance();
                BeanUtils.copyProperties(source, newInstance);
                return newInstance;
            } catch (Exception var3) {
                LogUtil.error(BeanTransUtil.class, var3);
                return null;
            }
        }
    }

    public static <T> List<T> convertList(List<?> source, Class<T> target) {
        return convertList(source, target, (BeanFunction)null);
    }

    public static <T> List<T> convertList(List<?> source, Class<T> target, BeanFunction<T> function) {
        if (source == null) {
            return null;
        } else if (target == null) {
            return null;
        } else if (source.size() == 0) {
            return Collections.emptyList();
        } else {
            try {
                List<T> result = new ArrayList();

                Object newInstance;
                for(Iterator var4 = source.iterator(); var4.hasNext(); result.add((T) newInstance)) {
                    Object obj = var4.next();
                    newInstance = target.newInstance();
                    BeanUtils.copyProperties(obj, newInstance);
                    if (function != null) {
                        function.invok((T) newInstance);
                    }
                }

                return result;
            } catch (Exception var7) {
                LogUtil.error(BeanTransUtil.class, var7);
                return null;
            }
        }
    }

    public static <N, O> List<N> convertLists(List<O> source, Class<N> target, BeanFunctions<N, O> function) {
        if (source == null) {
            return null;
        } else if (target == null) {
            return null;
        } else if (source.size() == 0) {
            return Collections.emptyList();
        } else {
            try {
                List<N> result = new ArrayList();
                Iterator var4 = source.iterator();

                while(var4.hasNext()) {
                    O obj = (O) var4.next();
                    N newInstance = target.newInstance();
                    BeanUtils.copyProperties(obj, newInstance);
                    function.invok(newInstance, obj);
                    result.add(newInstance);
                }

                return result;
            } catch (Exception var7) {
                LogUtil.error(BeanTransUtil.class, var7);
                return null;
            }
        }
    }

    public interface BeanFunctions<N, O> {
        void invok(N n, O o);
    }

    public interface BeanFunction<T> {
        void invok(T obj);
    }
}
